module fft_wrap

!----------------------------------------------
! VARIABLES
!----------------------------------------------

  use dimensions, only : jx, jz, jv

  integer(kind=8), parameter :: FFTW_ESTIMATE = 0
  integer(kind=8), parameter :: FFTW_MEASURE  = 1
  integer(kind=8), parameter :: FFTW_FORWARD = -1
  integer(kind=8), parameter :: FFTW_BACKWARD = 1

  integer(kind=8) ::  plan_xforward,  plan_xbackward
  integer(kind=8) ::  plan_zforward,  plan_zbackward
  integer(kind=8) ::  plan_vforward,  plan_vbackward


  complex ::  xcopy(jx), zcopy(jz), vcopy(jv)

!----------------------------------------------
! SUBROUTINES
!----------------------------------------------


contains


!-- fftw initialization --

subroutine fft_init()

  implicit none

  call fftw_f77_create_plan(plan_xforward, jx, FFTW_FORWARD, FFTW_ESTIMATE)
  call fftw_f77_create_plan(plan_zforward, jz, FFTW_FORWARD, FFTW_ESTIMATE)
  call fftw_f77_create_plan(plan_vforward, jv, FFTW_FORWARD, FFTW_ESTIMATE)

  call fftw_f77_create_plan(plan_xbackward, jx, FFTW_BACKWARD, FFTW_ESTIMATE)
  call fftw_f77_create_plan(plan_zbackward, jz, FFTW_BACKWARD, FFTW_ESTIMATE)
  call fftw_f77_create_plan(plan_vbackward, jv, FFTW_BACKWARD, FFTW_ESTIMATE)

end subroutine




!-- forward transform --

subroutine fft_forward(n, datain, dataout)

  implicit none

  integer  n
  complex  datain(:)
  complex  dataout(:)


  if (n .eq. jx) then

     call fftw_f77_one(plan_xforward, datain, xcopy)
     dataout = xcopy

  else if (n .eq. jz) then

     call fftw_f77_one(plan_zforward, datain, zcopy)
     dataout = zcopy

  else if (n .eq. jv) then

     call fftw_f77_one(plan_vforward, datain, vcopy)
     dataout = vcopy

  else

     write (*,*) "wrond size of data"

  end if

end subroutine



!-- forward transform --

subroutine fft_backward(n, datain, dataout)

  implicit none

  integer  n
  complex  datain(:)
  complex  dataout(:)


  if (n .eq. jx) then

     call fftw_f77_one(plan_xbackward, datain, xcopy)
     dataout = xcopy

  else if (n .eq. jz) then

     call fftw_f77_one(plan_zbackward, datain, zcopy)
     dataout = zcopy

  else if (n .eq. jv) then

     call fftw_f77_one(plan_vbackward, datain, vcopy)
     dataout = vcopy

  else

     write (*,*) "wrond size of data"

  end if


end subroutine

end module
